﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ThouShaltNotBrick.Logic.Helpers;
using Microsoft.Xna.Framework;
using GameUtils;

namespace ThouShaltNotBrick.Logic.GeneralObjects
{
    public sealed class LevelWithCurrentState
    {
        #region Properties
        #region Pad
        /// <summary>
        /// Pad property.
        /// </summary>
        public Pad Pad { get; private set; }
        #endregion

        #region Ball
        /// <summary>
        /// Ball property.
        /// </summary>
        public List<Ball> Balls { get; private set; }
        #endregion

        #region Bricks
        /// <summary>
        /// Bricks property.
        /// </summary>
        public BrickWithCurrentState[,] Bricks { get; private set; }
        #endregion

        private Level mLevel;
        #endregion

        #region Constructors
        public LevelWithCurrentState(
            Level pLevel)
        {
            Initialize(pLevel);
        }
        #endregion

        #region Methods
        private void Initialize(
            Level pLevel)
        {
            mLevel = pLevel;

            Balls = new List<Ball>();

            ResetPadAndBall();

            Bricks = new BrickWithCurrentState[Constants.BRICK_ROWS, Constants.BRICK_COLUMNS];
            List<Brick> brickList = pLevel.GetAllBricks();
            foreach (Brick brick in brickList)
            {
                Bricks[brick.Row, brick.Column] = new BrickWithCurrentState(brick);
            }
        }

        public void ResetPadAndBall()
        {
            Balls.Clear();

            Pad = new Pad(Constants.PAD_INITIAL_POSITION_X);
            Ball ball = new Ball(
                new Vector2(Constants.BALL_INITIAL_POSITION_X, Constants.BALL_INITIAL_POSITION_Y),
                new PolarVector2(Constants.BALL_SPEED, Constants.BALL_INITIAL_TRAJECTORY_ANGLE),
                Constants.BALL_RADIUS);

            Balls.Add(ball);
        }

        public bool IsLevelClear()
        {
            for (int i = 0; i < Constants.BRICK_ROWS; i++)
            {
                for (int j = 0; j < Constants.BRICK_COLUMNS; j++)
                {
                    if (BrickHelpers.IsBrickExisting(Bricks[i, j]))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
        #endregion
    }
}